/*
** expulse.c for Expulse in /home/vriely/Dropbox/work/Unix/Zappy_rendu/serv-src
**
** Made by Samuel Vrielynck
** Login   <vriely_s@epitech.net>
**
** Started on  Thu Jul 05 15:07:57 2012 Samuel Vrielynck
** Last update Wed Jul 18 19:36:53 2012 Samuel Vrielynck
*/

#include	<stdlib.h>
#include	"server.h"

static void	send_data(t_game *game, int sock)
{
  if (game->graphic != NULL)
  {
    game->graphic->send->concat(game->graphic->send, "pex ");
    game->graphic->send->concat_value(game->graphic->send, sock);
    game->graphic->send->concat(game->graphic->send, "\n");
    game->event_mnt->add_back(game->event_mnt, new_event(EXPULSE_MNT, \
	  0, NULL));
  }
}

char		handle_expulse(t_event *event, t_game *game, t_list *clients,
    long unsigned int elapsed_time)
{
  t_item	*cur;
  t_expulse	*expulse;
  t_gamer	*gamer;

  expulse = (struct s_expulse *)event->data;
  if (expulse->client && expulse->client->gamer &&
      is_alive(game->time, *(expulse->client->gamer), elapsed_time))
  {
    cur = clients->head;
    while (cur != NULL)
    {
      gamer = ((struct s_client *)cur->content)->gamer;
      if (gamer != NULL)
      {
	if (gamer->coord.x == expulse->client->gamer->coord.x &&
	    gamer->coord.y == expulse->client->gamer->coord.y &&
	    expulse->client->gamer != gamer)
	  move_player(game, gamer, expulse->client->gamer->direction);
      }
      cur = cur->next;
    }
    send_data(game, expulse->client->sock);
  }
  free(expulse);
  return (1);
}
